package com.ysm.smfu.holder;

import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.RelativeLayout;

import com.ysm.smfu.R;
import com.ysm.smfu.adapter.DefaultAdapter;
import com.ysm.smfu.utils.UIUtils;

public class MoreHolder extends BaseHolder<Integer> {
    public static final int HAS_MORE = 0;//有更多数据
    public static final int ERROR = 1;//加载更多失败
    public static final int HAS_NO_MORE = 2;//没有更多数据
    private DefaultAdapter adapter;
    private boolean hasMore;
    private RelativeLayout rl_more_loading;
    private RelativeLayout rl_more_error;
    private RelativeLayout rl_more_nomore;
    private Integer state;

    public MoreHolder(DefaultAdapter adapter, boolean hasMore) {

        this.adapter = adapter;
        this.hasMore = hasMore;
        if (!hasMore) {
            //没有更多数据 自动隐藏加载更多条目
            setData(HAS_NO_MORE);
        }
    }

    @Override
    protected View initView() {
        View view = UIUtils.inflate(R.layout.item_more);
        rl_more_loading = (RelativeLayout) view.findViewById(R.id.rl_more_loading);
        rl_more_error = (RelativeLayout) view.findViewById(R.id.rl_more_error);
        rl_more_nomore = (RelativeLayout) view.findViewById(R.id.rl_more_nomore);
        return view;
    }

    @Override
    public View getContentView() {
        // 就是加载更多条目显示的时候 加载跟多
        if (hasMore) {//  有更多数据 才去调用加载更多
            setData(HAS_MORE);
            onload();
        }

        return super.getContentView();
    }

    private void onload() {
        //加载更多数据
        if (adapter != null) {
        }
    }

    @Override
    protected void refreshView(Integer state) {
        this.state = state;

        if (rl_more_loading != null) {
            rl_more_loading.setVisibility(state == HAS_MORE ? View.VISIBLE : View.GONE);
        }
        if (rl_more_error != null) {
            rl_more_error.setVisibility(state == ERROR ? View.VISIBLE : View.GONE);
        }
        if (rl_more_nomore != null) {
            rl_more_nomore.setVisibility(state == HAS_NO_MORE ? View.VISIBLE : View.GONE);
            handler.sendEmptyMessageDelayed(100, 500);
        }
    }

    public void refresh() {
        if (state == ERROR) {
            if (adapter != null) {
                setData(HAS_MORE);
            }
        }
    }


    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
            alphaAnimation.setDuration(500);
            alphaAnimation.setFillAfter(true);
            rl_more_nomore.startAnimation(alphaAnimation);
            alphaAnimation.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {

                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    rl_more_nomore.setVisibility(View.GONE);
                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }
            });

        }
    };

}
